package com.elinkway.infinitemovies.bigdata;

import android.text.TextUtils;

import com.lvideo.http.bean.LVideoBaseBean;
import com.lvideo.http.bean.LVideoDataHull;

/**
 * 推送上报
 */
public class BigDataPushRecvTask extends BigDataBaseTask {
    private String ap_type;
    private String ap_name;

    /**
     * 设置apType
     *
     * @param apType<br> {@link BigDataConstants#AP_VAL_PUSH_TYPE_ALBUM},
     *                   {@link BigDataConstants#AP_VAL_PUSH_TYPE_PAGE},
     *                   {@link BigDataConstants#AP_VAL_PUSH_TYPE_THEME},
     *                   {@link BigDataConstants#AP_VAL_PUSH_TYPE_VIDEO},
     * @return
     */
    public BigDataPushRecvTask setApType(String apType) {
        ap_type = apType;
        return this;
    }

    public BigDataPushRecvTask setApName(String name) {
        ap_name = name;
        return this;
    }

    public BigDataPushRecvTask setPid(String pid) {
        putParam("pic", pid);
        return this;
    }

    private String getAp() {
        StringBuilder ap = new StringBuilder();
        ap.append(BigDataConstants.AP_KEY_PUSH_TYPE)
                .append("=")
                .append(ap_type)
                .append("&")
                .append(BigDataConstants.AP_KEY_PUSH_NAME)
                .append("=")
                .append(ap_name);

        return ap.toString();
    }

    /**
     * 事件结果
     *
     * @param ar {@link BigDataConstants#AR_FAILED},
     *           {@link BigDataConstants#AR_SUCCESS}
     * @return
     */
    public BigDataPushRecvTask setAR(String ar) {
        putParam("ar", ar);
        return this;
    }

    /**
     * 设置网络类型
     *
     * @param netType
     * @return
     */
    public BigDataPushRecvTask setNT(String netType) {
        putParam("nt", netType);
        return this;
    }

    /**
     * 检测参数
     *
     * @return
     */
    public boolean checkArgs() {
        String message = "";
        if (TextUtils.isEmpty(getParam("ar"))) {
            message = "BigDataPushRecvTask: ar is null,  call setAR(ar) first.";
        } else if (TextUtils.isEmpty(ap_type)) {
            message = "BigDataPushRecvTask: ap is null, you should call following methods : setApType(ap)";
        } else if (TextUtils.isEmpty(ap_name)) {
            message = "BigDataPushRecvTask: name is null, call setApName(name) first. ";
        } else if (TextUtils.isEmpty(getParam("nt"))) {
            message = "BigDataPushRecvTask: nt is null, call setNT(nt) first. ";
        }
        if (!TextUtils.isEmpty(message)) {
            LogUtils.w(message);
            return false;
        }
        return true;
    }

    @Override
    public boolean execute() {
        if (checkArgs()) {
            putParam("ap", getAp());
            putParam("acode", BigDataConstants.ACODE_PUSH);
            LVideoDataHull<LVideoBaseBean> d = BigDataApi.postPushAction(null, getAp()
                    , getParam("ar"), BigDataConstants.ACODE_PUSH, getParam("cid", "-"), getParam("vid", "-"), getParam("pid", "-"), getParam("nt"));
            return d.getDataType() == LVideoDataHull.DataType.DATA_PARSER_IS_NULL;
        }
        return false;
    }


}
